using AlibabaCloud.SDK.Dingtalkcard_1_0.Models;
using AlibabaCloud.SDK.Dingtalkdevicemng_1_0.Models;
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using Tea;
using static dingtalk_Card.dingtalk_CardServerCommand.dongtaishuju;
using static System.Net.WebRequestMethods;

namespace dingtalk_Card
{
    [Icon("pack://application:,,,/dingtalk_Card;component/Resources/Icon.png")]
    [Designer("dingtalk_Card.Designer.dingtalk_CardServerCommandDesigner, dingtalk_Card")]
    public class dingtalk_CardServerCommand : Command, ICommandExecutableInServerSideAsync
    {
        [Required]
        [Description("必填项--请选择使用场景，当前只能单个场景使用。")]
        [ComboProperty(ValueList = "创建|更新|投放|注册|创建并投放", DisplayList = "创建卡片|更新卡片|投放卡片|注册回调|创建并投放卡片")]  //直接指定的审批人列表approvers
        public string 选择功能 { get; set; } = "创建并投放";

        [Required]
        [Description("必填项--请选择使用场景，当前只能单个场景使用。")]
        [ComboProperty(ValueList = "IM_GROUP|IM_SINGLE|IM_ROBOT|ONE_BOX|COOPERATON_FEED|DOC", DisplayList = "群聊|酷应用|机器人|吊顶|协作|文档")]  //直接指定的审批人列表approvers
        public string 选择使用场景 { get; set; } = "IM_GROUP";


        [Description("必填--调用获取企业内部应用的accessToken接口获取。")]
        [DisplayName("访问凭证accessToken")]
        [Required]
        [FormulaProperty] public object aa1 { get; set; }

        [Description("非必填--卡片创建者的userId")]
        [DisplayName("卡片创建者的userId")]
        [FormulaProperty] public object aa2 { get; set; }

        [Description("必填--可通过登录开发者后台 > 卡片平台获取。")]
        [DisplayName("卡片内容模板ID")]
        [Required]
        [FormulaProperty] public object aa3 { get; set; }

        [Description("必填--自定义字符串作为参数传递给钉钉用于记录任务id。注意不能使用纯数字")]
        [DisplayName("外部卡片实例Id")]
        [Required]
        [FormulaProperty] public object aa4 { get; set; }

        [Description("非必填--卡片回调的类型。")]
        [ComboProperty(ValueList = "STREAM|HTTP", DisplayList = "stream模式|http模式")]  //直接指定的审批人列表approvers
        public string 回调类型 { get; set; }

        [Description("非必填--企业内部应用可通过调用服务端API-注册卡片回调地址接口，根据填写的callbackRouteKey入参字段获取。")]
        [DisplayName("回调HTTP模式时的Key")]
        [FormulaProperty] public object aa5 { get; set; }

        [Description("非必填--卡片模板内容替换参数")]
        [ListProperty] public List<kuozhan> 卡片模板内容替换参数 { get; set; }



        [Description("非必填--用户的私有数据")]
        [ListProperty] public List<siyoushuju> 用户私有数据 { get; set; }
        public class siyoushuju : ObjectPropertyBase
        {
            [FormulaProperty] public object 用户userId { get; set; }

            [Description("非必填--用户的私有数据")]
            [ListProperty] public List<kuozhan> 用户私有数据内容 { get; set; }

        }

        [Description("非必填--动态数据源配置列表")]
        [ListProperty] public List<dongtaishuju> 动态数据源配置 { get; set; }
        public class dongtaishuju : ObjectPropertyBase
        {
            [FormulaProperty] public object 数据源ID { get; set; }

            [Description("非必填--用户的私有数据")]
            [ListProperty] public List<kuozhan> 回调固定参数 { get; set; }

            [ObjectProperty(ObjType = typeof(dongtailaqu))]
            public dongtailaqu 动态数据源配置 { get; set; }
            public class dongtailaqu : ObjectPropertyBase
            {
                [Description("使用动态数据源功能时该参数字段必填。")]
                [ComboProperty(ValueList = "NONE|INTERVAL|ONCE", DisplayList = "不拉取|间隔拉取|只拉取一次")]  //直接指定的审批人列表approvers
                public string 拉取策略 { get; set; } = "INTERVAL";

                [Description("单位秒--只在将拉取策略设置为间隔拉取的时候生效。最小拉取间隔时间3秒")]
                [IntProperty(Min = 3, Max = 999)]
                public int 拉取的间隔时间 { get; set; }

                [Description("使用动态数据源功能时该参数字段必填。")]
                [ComboProperty(ValueList = "SECONDS|MINUTES|HOURS|DAYS", DisplayList = "秒|分钟|小时|天")]  //直接指定的审批人列表approvers
                public string 拉取间隔的时间单位 { get; set; }

                // 等于时则可见
                public override bool GetDesignerPropertyVisible(string propertyName)
                {
                    if (propertyName == nameof(拉取的间隔时间) || propertyName == nameof(拉取间隔的时间单位))
                    {
                        return this.拉取策略 == "INTERVAL";
                    }
                    return base.GetDesignerPropertyVisible(propertyName);
                }
            }
        }
        [ObjectProperty(ObjType = typeof(SharedChatConfig))]
        public SharedChatConfig 酷应用配置 { get; set; }

        [ObjectProperty(ObjType = typeof(SharedChatConfig))]
        public SharedChatConfig 群聊配置 { get; set; }

        [ObjectProperty(ObjType = typeof(SharedChatConfig))]
        public SharedChatConfig 机器人配置 { get; set; }

        [ObjectProperty(ObjType = typeof(xiezuo))]
        public xiezuo 协作场域配置 { get; set; }
        public class xiezuo : ObjectPropertyBase
        {
            [FormulaProperty] public object 卡片标题 { get; set; }
            [FormulaProperty] public object 酷应用编码 { get; set; }
        }


        [Description("使用吊顶场景该字段必填。")]
        [FormulaProperty] public object 吊顶配置 { get; set; }


        [ObjectProperty(ObjType = typeof(danliaokutoufang))]
        public danliaokutoufang 酷应用投放参数 { get; set; }
        public class danliaokutoufang : ObjectPropertyBase
        {
            // 引用 atyonghu 类中的 AT 的用户列表
            [ListProperty] public List<AtUser> AT用户 { get; set; }

            // 引用 kuozhan 类中的 扩展字段
            [ListProperty] public List<kuozhan> 扩展字段 { get; set; }

        }


        [ObjectProperty(ObjType = typeof(qunliao))]
        public qunliao 群聊投放参数 { get; set; }
        public class qunliao : ObjectPropertyBase
        {
            [Description("必填项--场景群机器人发送群聊使用群机器人robotCode，非场景群的企业内部开发的机器人发送群聊，使用机器人的AppKey，第三方企业机器人，使用机器人的robotCode。")]
            [FormulaProperty] public object 机器人robotCode { get; set; }

            // 引用 atyonghu 类中的 AT 的用户列表
            [ListProperty] public List<AtUser> AT用户 { get; set; }

            // 引用 kuozhan 类中的 扩展字段
            [ListProperty] public List<kuozhan> 扩展字段 { get; set; }

            [ListProperty] public List<qunjieshouren> 接收人userId列表 { get; set; }
            public class qunjieshouren : ObjectPropertyBase
            {
                [FormulaProperty] public object userId { get; set; }
            }

        }

        [ObjectProperty(ObjType = typeof(imjqrtoufang))]
        public imjqrtoufang 机器人投放参数 { get; set; }
        public class imjqrtoufang : ObjectPropertyBase
        {
            [Description("IM机器人单聊若未设置其他投放属性，需设置spaeType为IM_ROBOT")]
            [FormulaProperty] public object spaceType { get; set; }

            [Description("必填项--场景群机器人发送群聊使用群机器人robotCode，非场景群的企业内部开发的机器人发送群聊，使用机器人的AppKey，第三方企业机器人，使用机器人的robotCode。")]
            [FormulaProperty] public object 机器人robotCode { get; set; }
            // 引用 kuozhan 类中的 扩展字段
            [ListProperty] public List<kuozhan> 扩展字段 { get; set; }
        }

        [ObjectProperty(ObjType = typeof(diaodingtoufang))]
        public diaodingtoufang 吊顶投放参数 { get; set; }
        public class diaodingtoufang : ObjectPropertyBase
        {
            [Description("必填项-13位时间戳")]
            [FormulaProperty] public object 过期时间戳 { get; set; }

            [ListProperty] public List<userid> 可以查看卡片的用户列表 { get; set; }

            [ListProperty] public List<xuanzeshebei> 可以查看吊顶卡片的设备 { get; set; }
            public class xuanzeshebei : ObjectPropertyBase
            {
                [Description("必填项--请选择使用场景，当前只能单个场景使用。")]
                [ComboProperty(ValueList = "android|ios|win|mac", DisplayList = "安卓|苹果IOS|微软Windows|苹果mac")]  //直接指定的审批人列表approvers
                public string 选择设备 { get; set; }
            }
        }

        [ObjectProperty(ObjType = typeof(xiezuotoufang))]
        public xiezuotoufang 协作投放参数 { get; set; }
        public class xiezuotoufang : ObjectPropertyBase
        {
            [Description("必填项-需要先申请在协作中投放该bizTag，申请通过后才能使用")]
            [FormulaProperty] public object 业务标识 { get; set; }

            [Description("必填项-13位时间戳")]
            [FormulaProperty] public object 排序时间 { get; set; }
        }

        [ObjectProperty(ObjType = typeof(userid))]
        public userid 文档投放参数 { get; set; }

        [Required]
        [Description("必填项--只填写SpaceId即可,官方文档：https://open.dingtalk.com/document/isvapp/create-and-deliver-cards")]
        [ListProperty] public List<changjingid> 场域id { get; set; }
        public class changjingid : ObjectPropertyBase
        {
            [FormulaProperty] public object SpaceId { get; set; }
        }


        [Description("非必填项--默认userId模式")]
        [ComboProperty(ValueList = "1|2", DisplayList = "userId模式|unionId模式")]  //直接指定的审批人列表approvers
        public int 用户userId类型 { get; set; } = 1;

        [ObjectProperty(ObjType = typeof(huidiaoshezhi))]
        public huidiaoshezhi 回调参数设置 { get; set; }

        [ObjectProperty(ObjType = typeof(gengxinxuanxiang))]
        public gengxinxuanxiang 更新选项 { get; set; }


        [ResultToProperty]
        [DisplayName("返回结果")]
        public string ResultTo { get; set; } = "返回结果";
        [ResultToProperty]
        [DisplayName("返回组装卡片数据")]
        public string ResultTo1 { get; set; } = "卡片数据";

        // 等于时则可见aa2
        public override bool GetDesignerPropertyVisible(string propertyName, CommandScope commandScope)
        {
            if (propertyName == nameof(选择使用场景)) //卡片回调HTTP模式时的路由 Key，用于查询注册的 callbackUrl。
            {
                return this.选择功能 == "创建" || this.选择功能 == "投放" || this.选择功能 == "创建并投放";
            }
            if (propertyName == nameof(aa2) || propertyName == nameof(aa3) || propertyName == nameof(回调类型) || propertyName == nameof(动态数据源配置)|| propertyName == nameof(ResultTo1))
            {
                return this.选择功能 == "创建" || this.选择功能 == "创建并投放";
            }
            if (propertyName == nameof(aa4)) //外部卡片实例Id
            {
                return this.选择功能 == "创建" || this.选择功能 == "投放" || this.选择功能 == "更新" || this.选择功能 == "创建并投放";
            }
            if (propertyName == nameof(卡片模板内容替换参数) || propertyName == nameof(用户私有数据))
            {
                return this.选择功能 == "创建" || this.选择功能 == "更新" || this.选择功能 == "创建并投放";
            }
            if (propertyName == nameof(卡片模板内容替换参数) || propertyName == nameof(用户私有数据) || propertyName == nameof(场域id))
            {
                return this.选择功能 == "投放" || this.选择功能 == "创建并投放";
            }
            if (propertyName == nameof(aa5)) //外部卡片实例Id
            {
                return (this.选择功能 == "创建" || this.选择功能 == "注册" || this.选择功能 == "创建并投放") && this.回调类型 == "HTTP";
            }
            if (propertyName == nameof(回调参数设置)) 
            {
                return this.选择功能 == "注册";
            }
            if (propertyName == nameof(更新选项)) 
            {
                return this.选择功能 == "更新";
            }
            if (propertyName == nameof(酷应用配置))
            {
                return (this.选择功能 == "创建" || this.选择功能 == "创建并投放") && this.选择使用场景 == "IM_SINGLE";
            }
            if (propertyName == nameof(群聊配置))
            {
                return (this.选择功能 == "创建" || this.选择功能 == "创建并投放") && this.选择使用场景 == "IM_GROUP";
            }
            if (propertyName == nameof(机器人配置))
            {
                return (this.选择功能 == "创建" || this.选择功能 == "创建并投放") && this.选择使用场景 == "IM_ROBOT";
            }
            if (propertyName == nameof(吊顶配置))
            {
                return (this.选择功能 == "创建" || this.选择功能 == "创建并投放") && this.选择使用场景 == "ONE_BOX";
            }
            if (propertyName == nameof(协作场域配置))
            {
                return (this.选择功能 == "创建" || this.选择功能 == "创建并投放") && this.选择使用场景 == "COOPERATON_FEED";
            }


            if (propertyName == nameof(酷应用投放参数)) 
            {
                return (this.选择功能 == "投放" || this.选择功能 == "创建并投放") && this.选择使用场景 == "IM_SINGLE";
            }
            if (propertyName == nameof(群聊投放参数))
            {
                return (this.选择功能 == "投放" || this.选择功能 == "创建并投放") && this.选择使用场景 == "IM_GROUP";
            }
            if (propertyName == nameof(机器人投放参数))
            {
                return (this.选择功能 == "投放" || this.选择功能 == "创建并投放") && this.选择使用场景 == "IM_ROBOT";
            }
            if (propertyName == nameof(吊顶投放参数))
            {
                return (this.选择功能 == "投放" || this.选择功能 == "创建并投放") && this.选择使用场景 == "ONE_BOX";
            }
            if (propertyName == nameof(协作投放参数))
            {
                return (this.选择功能 == "投放" || this.选择功能 == "创建并投放") && this.选择使用场景 == "COOPERATON_FEED";
            }
            if (propertyName == nameof(文档投放参数))
            {
                return (this.选择功能 == "投放" || this.选择功能 == "创建并投放") && this.选择使用场景 == "DOC";
            }

            return base.GetDesignerPropertyVisible(propertyName, commandScope);
        }
       

        

        public async Task<ExecuteResult> ExecuteAsync(IServerCommandExecuteContext dataContext)
        {
            //设置数据为空
            
            string openSpaceId = null;//场域及其场域id
            string huidiaotype = null;//回调类型
            string huidiaotypekey = null;//回调key
            
            var token = (await dataContext.EvaluateFormulaAsync(aa1))?.ToString() ?? string.Empty;
            if(选择功能 == "创建")
            {    
                CreateCardRequest.CreateCardRequestImGroupOpenSpaceModel ImGroupOpenSpaceModel = null;//获取群聊配置
                CreateCardRequest.CreateCardRequestImRobotOpenSpaceModel imRobotOpenSpaceModel = null;// IM机器人单聊配置
                CreateCardRequest.CreateCardRequestCoFeedOpenSpaceModel coFeedOpenSpaceModel = null;//协作场域配置
                CreateCardRequest.CreateCardRequestTopOpenSpaceModel topOpenSpaceModel = null;//吊顶配置
                CreateCardRequest.CreateCardRequestImSingleOpenSpaceModel imSingleOpenSpaceModel = null;//IM 单聊酷应用场域信息

                var cardData = await chuangjian.CreateCardData(dataContext, 卡片模板内容替换参数);
                if (回调类型 != null)
                {
                    huidiaotype = 回调类型;
                    huidiaotypekey = (await dataContext.EvaluateFormulaAsync(aa5))?.ToString() ?? string.Empty;
                }
                //判断场景
                if (选择使用场景 == "IM_GROUP") //获取群聊配置
                {
                    ImGroupOpenSpaceModel = await chuangjian.imqunliaopeizhi(dataContext, 群聊配置);
                }
                if (选择使用场景 == "IM_SINGLE")//获取IM酷应用单聊配置
                {
                    imSingleOpenSpaceModel = await chuangjian.imdanliaopeizhi(dataContext, 酷应用配置);
                }
                if (选择使用场景 == "IM_ROBOT")//获取IM机器人单聊配置
                {
                    imRobotOpenSpaceModel = await chuangjian.imjiqirenpeizhi(dataContext, 机器人配置);
                }
                if (选择使用场景 == "ONE_BOX")//吊顶配置
                {
                    topOpenSpaceModel = await chuangjian.getdiaoding(dataContext, 吊顶配置);
                }
                if (选择使用场景 == "COOPERATON_FEED")//协作配置
                {
                    coFeedOpenSpaceModel = await chuangjian.getxiezuopeizhi(dataContext, 协作场域配置);
                }
                AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateCardRequest createCardRequest = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateCardRequest
                {
                    UserId = (await dataContext.EvaluateFormulaAsync(aa2))?.ToString() ?? string.Empty,
                    CardTemplateId = (await dataContext.EvaluateFormulaAsync(aa3))?.ToString() ?? string.Empty,
                    OutTrackId = (await dataContext.EvaluateFormulaAsync(aa4))?.ToString() ?? string.Empty,
                    CallbackType = huidiaotype,
                    CallbackRouteKey = huidiaotypekey,
                    CardData = cardData,
                    PrivateData = await chuangjian.ProcessUserPrivateDataAsync(dataContext, 用户私有数据),
                    OpenDynamicDataConfig = await chuangjian.dongtaishujuyuan(dataContext, 动态数据源配置),
                    ImGroupOpenSpaceModel = ImGroupOpenSpaceModel,
                    ImSingleOpenSpaceModel = imSingleOpenSpaceModel,
                    ImRobotOpenSpaceModel = imRobotOpenSpaceModel,
                    CoFeedOpenSpaceModel = coFeedOpenSpaceModel,
                    TopOpenSpaceModel = topOpenSpaceModel,
                    UserIdType = 用户userId类型,
                };
                try
                {
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Client aaabbb = dingcard.CreateClient();
                    var createCardHeaders = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateCardHeaders();
                    createCardHeaders.XAcsDingtalkAccessToken = token;
                    var res = aaabbb.CreateCardWithOptions(createCardRequest, createCardHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                    dataContext.Parameters[ResultTo] = res.Body;
                }
                catch (TeaException err)
                {
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = err.Message;
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _err.Message }
                    });
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = dingcard.CreateErrorJson(int.Parse(err.Code), err.Message);
                    }
                }
                dataContext.Parameters[ResultTo1] = cardData; // 把卡片数据组装结果返回
            }
            else if (选择功能 == "投放")
            {
                DeliverCardRequest.DeliverCardRequestImGroupOpenDeliverModel imGroupOpenDeliverModel = null; //群聊投放参数
                DeliverCardRequest.DeliverCardRequestImSingleOpenDeliverModel imSingleOpenDeliverModel = null;//单聊酷应用投放参数
                DeliverCardRequest.DeliverCardRequestImRobotOpenDeliverModel imRobotOpenDeliverModel = null;//IM机器人单聊投放参数
                DeliverCardRequest.DeliverCardRequestTopOpenDeliverModel topOpenDeliverModel = null;//吊顶投放参数
                DeliverCardRequest.DeliverCardRequestCoFeedOpenDeliverModel coFeedOpenDeliverModel = null;//协作投放参数
                DeliverCardRequest.DeliverCardRequestDocOpenDeliverModel docOpenDeliverModel = null;//文档投放参数
                //判断场景
                if (选择使用场景 == "IM_GROUP") //获取群聊配置
                {
                    imGroupOpenDeliverModel = await toufang.qunliaotoufang(dataContext, 群聊投放参数);
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "IM_SINGLE")//获取IM酷应用单聊配置
                {
                    imSingleOpenDeliverModel = await toufang.danliaokuyingyong(dataContext, 酷应用投放参数);
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "IM_ROBOT")//获取IM机器人单聊配置
                {
                    imRobotOpenDeliverModel = await toufang.imjiqirendanliaotoufang(dataContext, 机器人投放参数);
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "ONE_BOX")//吊顶配置
                {
                    topOpenDeliverModel = await toufang.diaodingtoufang(dataContext, 吊顶投放参数);
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "COOPERATON_FEED")//协作配置
                {
                    coFeedOpenDeliverModel = await toufang.xiezuotoufangcanshu(dataContext, 协作投放参数);
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "DOC")//文档配置
                {
                    docOpenDeliverModel = await toufang.wendangtoufangcanshu(dataContext, 文档投放参数);
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }

                AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest deliverCardRequest = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardRequest
                {
                    OutTrackId = (await dataContext.EvaluateFormulaAsync(aa4))?.ToString() ?? string.Empty,
                    OpenSpaceId = openSpaceId,
                    ImSingleOpenDeliverModel = imSingleOpenDeliverModel,
                    ImRobotOpenDeliverModel = imRobotOpenDeliverModel,
                    ImGroupOpenDeliverModel = imGroupOpenDeliverModel,
                    TopOpenDeliverModel = topOpenDeliverModel,
                    CoFeedOpenDeliverModel = coFeedOpenDeliverModel,
                    DocOpenDeliverModel = docOpenDeliverModel,
                    UserIdType = 用户userId类型,
                };
                try
                {
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Client aacc = dingcard.CreateClient();
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardHeaders deliverCardHeaders = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.DeliverCardHeaders();
                    deliverCardHeaders.XAcsDingtalkAccessToken = token;
                    var res = aacc.DeliverCardWithOptions(deliverCardRequest, deliverCardHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                    dataContext.Parameters[ResultTo] = res.Body;
                }
                catch (TeaException err)
                {
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = err.Message;
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _err.Message }
                    });
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = dingcard.CreateErrorJson(int.Parse(err.Code), err.Message);
                    }
                }
                
            }
            else if (选择功能 == "更新")
            {
                var cardUpdateOptions =  gengxin.gengxinpeizhi(更新选项.卡片数据更新方式, 更新选项.私有数据更新方式);
                AlibabaCloud.SDK.Dingtalkcard_1_0.Models.UpdateCardRequest updateCardRequest = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.UpdateCardRequest
                {
                    OutTrackId = (await dataContext.EvaluateFormulaAsync(aa4))?.ToString() ?? string.Empty,
                    CardData = await gengxin.CreateCardData(dataContext, 卡片模板内容替换参数),
                    PrivateData = await dingcard.ProcessUserPrivateDataAsync(dataContext, 用户私有数据),
                    CardUpdateOptions = cardUpdateOptions,
                    UserIdType = 用户userId类型,
                };
                try
                {
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Client aaaddd = dingcard.CreateClient();
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Models.UpdateCardHeaders updateCardHeaders = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.UpdateCardHeaders();
                    updateCardHeaders.XAcsDingtalkAccessToken = token;
                    var res = aaaddd.UpdateCardWithOptions(updateCardRequest, updateCardHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                    dataContext.Parameters[ResultTo] = res.Body;
                }
                catch (TeaException err)
                {
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = err.Message;
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _err.Message }
                    });
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = dingcard.CreateErrorJson(int.Parse(err.Code), err.Message);
                    }
                }
            }
            else if (选择功能 == "注册")
            {
                AlibabaCloud.SDK.Dingtalkcard_1_0.Models.RegisterCallbackHeaders registerCallbackHeaders = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.RegisterCallbackHeaders();
                registerCallbackHeaders.XAcsDingtalkAccessToken = token;
                try
                {
                    var huidiaobody = await dingcard.huidiaozhuce(dataContext, 回调参数设置);
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Client aaafff = dingcard.CreateClient();
                    var res = aaafff.RegisterCallbackWithOptions(huidiaobody, registerCallbackHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                    dataContext.Parameters[ResultTo] = res.Body;
                }
                catch (TeaException err)
                {
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = err.Message;
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                    {
                        { "message", _err.Message }
                    });
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = dingcard.CreateErrorJson(int.Parse(err.Code), err.Message);
                    }
                }
            }
            else if (选择功能 == "创建并投放")
            {
                if (回调类型 != null)
                {
                    huidiaotype = 回调类型;
                    huidiaotypekey = (await dataContext.EvaluateFormulaAsync(aa5))?.ToString() ?? string.Empty;
                }
                // 获取卡片数据
                var cardData = await dingcard.CreateCardData(dataContext, 卡片模板内容替换参数);
                //获取用户私有数据
                var result0 = await dingcard.ProcessUserPrivateDataAsync(dataContext, 用户私有数据);
                //获取动态数据源
                var result1 = await dingcard.dongtaishujuyuan(dataContext, 动态数据源配置);

                CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenSpaceModel result3 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel result8 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenSpaceModel result2 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestImSingleOpenDeliverModel result7 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel result4 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel result9 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestTopOpenSpaceModel result6 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestTopOpenDeliverModel result10 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenSpaceModel result5 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestCoFeedOpenDeliverModel result11 = null;
                CreateAndDeliverRequest.CreateAndDeliverRequestDocOpenDeliverModel result12 = null;
                //判断场景
                if (选择使用场景 == "IM_GROUP")
                {
                    //获取群聊配置
                    result3 = await dingcard.imqunliaopeizhi(dataContext, 群聊配置);
                    //群聊投放参数
                    result8 = await dingcard.qunliaotoufang(dataContext, 群聊投放参数);
                    //场域及其场域id
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "IM_SINGLE")
                {
                    //获取IM酷应用单聊配置
                    result2 = await dingcard.imdanliaopeizhi(dataContext, 酷应用配置);
                    //单聊酷应用场域投放参数
                    result7 = await dingcard.danliaokuyingyong(dataContext, 酷应用投放参数);
                    //场域及其场域id
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "IM_ROBOT")
                {
                    //获取IM机器人单聊配置
                    result4 = await dingcard.imjiqirenpeizhi(dataContext, 机器人配置);
                    //IM机器人单聊投放参数
                    result9 = await dingcard.imjiqirendanliaotoufang(dataContext, 机器人投放参数);
                    //场域及其场域id
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "ONE_BOX")
                {
                    //吊顶配置
                    result6 = await dingcard.getdiaoding(dataContext, 吊顶配置);
                    //吊顶投放参数
                    result10 = await dingcard.diaodingtoufang(dataContext, 吊顶投放参数);
                    //场域及其场域id
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "COOPERATON_FEED")
                {
                    //协作配置
                    result5 = await dingcard.getxiezuopeizhi(dataContext, 协作场域配置);
                    //协作投放参数
                    result11 = await dingcard.xiezuotoufangcanshu(dataContext, 协作投放参数);
                    //场域及其场域id
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                if (选择使用场景 == "DOC")
                {
                    //文档投放参数
                    result12 = await dingcard.wendangtoufangcanshu(dataContext, 文档投放参数);
                    //场域及其场域id
                    openSpaceId = await dingcard.getchangjingid(dataContext, 选择使用场景, 场域id);
                }
                //发送请求部分
                
                AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest createAndDeliverRequest = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverRequest
                {
                    UserId = (await dataContext.EvaluateFormulaAsync(aa2))?.ToString() ?? string.Empty, //卡片创建者的userId。
                    CardTemplateId = (await dataContext.EvaluateFormulaAsync(aa3))?.ToString() ?? string.Empty,//卡片内容模板ID
                    OutTrackId = (await dataContext.EvaluateFormulaAsync(aa4))?.ToString() ?? string.Empty,//外部卡片实例Id
                    CallbackType = huidiaotype,//卡片回调的类型
                    CallbackRouteKey = huidiaotypekey,//卡片回调HTTP模式时的路由 Key
                    CardData = cardData,//卡片数据
                    PrivateData = result0,//用户的私有数据
                    OpenDynamicDataConfig = result1,//动态数据源配置
                    ImSingleOpenSpaceModel = result2,//IM单聊酷应用场域信息
                    ImGroupOpenSpaceModel = result3,//IM群聊场域信息             
                    ImRobotOpenSpaceModel = result4,//IM机器人单聊场域信息
                    CoFeedOpenSpaceModel = result5,//协作场域信息
                    TopOpenSpaceModel = result6,//吊顶场域信息
                    OpenSpaceId = openSpaceId,//表示场域及其场域id
                    ImSingleOpenDeliverModel = result7,//IM单聊酷应用场域信息
                    ImGroupOpenDeliverModel = result8,//群聊投放参数
                    ImRobotOpenDeliverModel = result9,//IM机器人单聊投放参数
                    TopOpenDeliverModel = result10,//吊顶投放参数
                    CoFeedOpenDeliverModel = result11,//协作投放参数
                    DocOpenDeliverModel = result12,//文档投放参数
                    UserIdType = 用户userId类型,//用户userId类型
                };
                try
                {
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Client client = dingcard.CreateClient();
                    AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverHeaders createAndDeliverHeaders = new AlibabaCloud.SDK.Dingtalkcard_1_0.Models.CreateAndDeliverHeaders();
                    createAndDeliverHeaders.XAcsDingtalkAccessToken = token;
                    var res = client.CreateAndDeliverWithOptions(createAndDeliverRequest, createAndDeliverHeaders, new AlibabaCloud.TeaUtil.Models.RuntimeOptions());
                    dataContext.Parameters[ResultTo] = res.Body;
                }
                catch (TeaException err)
                {
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = err.Message; // 把计算的结果设置到结果变量中
                    }
                }
                catch (Exception _err)
                {
                    TeaException err = new TeaException(new Dictionary<string, object>
                {
                    { "message", _err.Message }
                });
                    if (!AlibabaCloud.TeaUtil.Common.Empty(err.Code) && !AlibabaCloud.TeaUtil.Common.Empty(err.Message))
                    {
                        // err 中含有 code 和 message 属性，可帮助开发定位问题
                        dataContext.Parameters[ResultTo] = dingcard.CreateErrorJson(int.Parse(err.Code), err.Message); // 把计算的结果设置到结果变量中
                    }
                }
                dataContext.Parameters[ResultTo1] = cardData; // 把卡片数据组装结果返回
            }

            return new ExecuteResult();
        }

        public override string ToString()
        {
            return "钉钉互动卡片";
        }

        public override CommandScope GetCommandScope()
        {
            return CommandScope.ExecutableInServer;
        }
    }

    public class yuyan : ObjectPropertyBase
    {
        [Description("使用动态数据源功能时该参数字段必填。")]
        [ComboProperty(ValueList = "ZH_CN|ZH_TW|EN_US|JA_JP|VI_VN", DisplayList = "简体中文|繁体中文|英文|日语|越南语")]
        public string 语言类别 { get; set; }

        [FormulaProperty] public object 参数值 { get; set; }
    }


    // 公共用户类
    public class AtUser : ObjectPropertyBase
    {
        [FormulaProperty] public object userId { get; set; }
        [FormulaProperty] public object 用户名 { get; set; }
    }

    // 公共扩展字段类
    public class kuozhan : ObjectPropertyBase
    {
        [FormulaProperty] public object 参数名 { get; set; }
        [FormulaProperty] public object 参数值 { get; set; }
    }


    public enum shujuleibie
    {
        [Description("自定义")]
        zidingyi,
        [Description("字符串")]
        Student,
        [Description("字符串数组")]
        Teacher,
        [Description("布尔值")]
        BooleanValue,
        [Description("布尔数组")]
        BooleanArray,
        [Description("数字")]
        Number,
        [Description("对象")]
        Object,
        [Description("对象数组")]
        ObjectArray,
        [Description("Markdown")]
        Markdown,
        [Description("图表")]
        Chart,
        [Description("用户信息")]
        UserInfo,
        [Description("下拉选项数组")]
        xialashuzu,
        [Description("表格")]
        Table,
        [Description("表单")]
        Form
    }
    // 公共扩展字段类
    public class userid : ObjectPropertyBase
    {
        [FormulaProperty] public object userId { get; set; }
    }
    // 更新选项
    public class gengxinxuanxiang : ObjectPropertyBase
    {
        [Description("true：按 key 更新 cardData 数据，false：覆盖更新 cardData 数据，不填默认覆盖更新。")]
        [DefaultValue(false)] public bool 卡片数据更新方式 { get; set; } = false;

        [Description("true：按 key 更新 privateData 数据，false：覆盖更新 privateData 数据，不填默认覆盖更新。")]
        [DefaultValue(false)] public bool 私有数据更新方式 { get; set; } = false;
    }
    public class SharedChatConfig : ObjectPropertyBase //组合参数
    {
        [Description("若使用imGroupOpenSpaceModel对象，则该字段必填。也就是使用IM群聊时候")]
        [DefaultValue(false)] public bool 是否支持转发 { get; set; } = false;

        [ListProperty] public List<yuyan> 卡片摘要 { get; set; }

        [FormulaProperty] public object 类型的icon { get; set; }

        [FormulaProperty] public object 卡片类型名 { get; set; }

        [Description("最大限制200个字符，超过存储截断200。")]
        [FormulaProperty] public object 供消息展示与搜索字段 { get; set; }

        [Description("若不填写则使用默认文案：如你收到1条新消息。")]
        [FormulaProperty] public object 通知内容 { get; set; }

        [DefaultValue(false)] public bool 是否关闭推送通知 { get; set; } = false;
    }
    public class huidiaoshezhi : ObjectPropertyBase
    {
        [Description("回调地址的路由Key,一个callbackRouteKey 仅可映射一个callbackUrl。")]
        [Required]
        [FormulaProperty] public object 回调路由Key { get; set; }

        [Description("接受动态卡片回调的 URL 地址,必须是公网可访问的 URL。")]
        [Required]
        [FormulaProperty] public object 回调URL地址 { get; set; }
        
        [Description("加密密钥用于校验来源。")]
        [FormulaProperty] public object 加密密钥 { get; set; }

        [Description("是否强制覆盖更新现有的 callbackRouteKey,强制覆盖更新回调地址，会影响到其他使用该 callbackRouteKey 的卡片")]
        [DefaultValue(false)] public bool 是否强制覆盖回调地址 { get; set; } = false;
    }
}
